package com.cty.controller;

import com.cty.pojo.Users;
import com.cty.service.UsersService;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @Auther: com.cty
 * @Date: 2020/3/22 18:24
 * @Description: 控制器
 * @version: 1.0
 */
@Controller
public class UsersController {
    Logger log = Logger.getLogger(UsersController.class);

    @Autowired
    private UsersService usersService;

    @RequestMapping("addUser")
    public String addUser(Users user, HttpServletRequest req) {
        log.debug("addUser: " + user.getUsername() + " " + user.getUserage());

        // 调用服务，向数据库添加一个用户
        this.usersService.addOneUser(user);

        // 将用户信息存入session作用域
        HttpSession session = req.getSession();
        session.setAttribute("user", user);

        // 转入主页面
        return "main";
    }

    @RequestMapping("selAll")
    public String selAll(HttpServletRequest req){
        log.debug("selAll");

        // 调用服务，查询所有用户
        List<Users> users = this.usersService.selectAllUser();

        // 将用户信息存入session作用域
        HttpSession session = req.getSession();
        session.setAttribute("users", users);

        // 转入显示页面
        return "page";
    }
}
